<template>
  <div id="build-group-chat-page">
    <h3>
      <van-icon
        name="arrow-left"
        color="rgb(107, 107, 107)"
        @click="routerGoBack"
      />
      <span>发起群聊</span>
    </h3>
    <div class="concent-top">
      <div class="search">
        <van-icon name="search" class="search-icon" />
        <!-- @input="onSearch" -->
        <input
          class="input"
          v-model="inputValue"
          type="text"
          placeholder="搜索"
        />
      </div>
      <p>选择一个群</p>
      <p>面对面建群</p>
      <p>企业微信联系人</p>
      <div class="friend-list">
        <van-checkbox-group
          v-model="selectBuildGroupFriends"
          direction="horizontal"
        >
          <div
            v-for="item in friendsBasicList"
            :key="item.phone"
            class="friend-item"
          >
            <van-checkbox :name="item.phone"></van-checkbox>
            <div class="img">
              <img :src="showImg(item.avatarurl)" alt="head" />
            </div>
            <div class="desc">
              <span>{{ item.nickname || item.phone }}</span>
            </div>
          </div>
        </van-checkbox-group>
      </div>
    </div>

    <p class="button" v-show="showSureButton">
      <button @click="isBuildGroup">
        完成({{ selectBuildGroupFriends.length }})
      </button>
    </p>
  </div>
</template>

<script>
import WebIM from "../../utils/WebIM";
export default {
  data() {
    return {
      inputValue: "",
      friendsBasicList: [],
      selectBuildGroupFriends: [],
      showSureButton: false,
    };
  },
  created() {
    this.getFriendsBasicList();
  },
  watch: {
    selectBuildGroupFriends(newV) {
      if (newV.length !== 0) {
        this.showSureButton = true;
      } else {
        this.showSureButton = false;
      }
    },
  },
  methods: {
    routerGoBack() {
      this.$router.go(-1);
    },
    getFriendsBasicList() {
      console.log(this.$store);
      WebIM.conn.getContacts(this.$store.state.user.userId).then((res) => {
        res.data.map((item) => {
          WebIM.conn.fetchUserInfoById(item).then((obj) => {
            // console.log(obj.data[item]);
            this.friendsBasicList.push(obj.data[item]);
          });
        });
        console.log(this.friendsBasicList);
      });
    },
    showImg(img) {
      if (img != undefined) {
        return img;
      } else {
        return require("../../assets/image/addressImg/head.webp");
      }
    },
    isBuildGroup() {
      // 超过两人才能建群
      if(this.selectBuildGroupFriends.length>1){
          let obj = [
        this.$store.state.user.userId,
        ...this.selectBuildGroupFriends,
      ];
     let option = {
        data: {
          // 群组名称
          groupname: `群聊(${this.selectBuildGroupFriends.length+1})`,
          // 群组描述
          desc: "A description of a group",
          // 群成员的用户 ID 组成的数组
          members: obj,
          /*
          是否为公开群：- true：是；- false：否。该群组为私有群。 */
          public: true,
          // 入群申请是否需群主或管理员审批 - true：需要；- false：不需要  私有群只能通过邀请方式进群
          approval: false,
          // 是否允许普通群成员邀请人入群：- true：允许；- false：不允许 只有群主和管理员才可以向群组添加用户。
          allowinvites: true,
          // 邀请加群时是否需要受邀用户确认： - true：受邀用户需同意才会加入群组；- false：受邀用户直接加入群组，无需确认。
          inviteNeedConfirm: false,
          // 群组最大成员数。
          maxusers: 500,
        },
      };
      // 创建群组
      WebIM.conn.createGroup(option).then((res)  => {
        this.$store.commit("setGroupId", res.data.groupid);
        console.log(res,"time");
        this.$router.push({
          name: "newGroupChatPage",
          params: {
            groupId: res.data.groupid,
          },
        });
      });
 
      } else {
        this.$toast.fail("建立群聊必须两人以上")
      }
         // 获取当前用户的创建和加入的群组
      WebIM.conn
        .getJoinedGroups({
          pageNum: 1,
          pageSize: 20,
          needAffiliations: false,
          needRole: false,
        })
        .then((res) => {
          // 解散所有群组(调试用)
          res.data.map((item) => {
            let option1 = {
              groupId: item.groupid,
            };
            WebIM.conn.destroyGroup(option1).then((r) => console.log(r));
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
#build-group-chat-page {
  width: 100vw;
  height: 100vh;
  color: rgb(164, 164, 164);
  background-color: var(--theme-background-black);
  h3 {
    background-color: rgb(67, 67, 67);
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 10px;
    height: 45px;
    span {
      width: 58%;
    }
  }

  .concent-top {
    width: 100%;
    .search {
      width: 100%;
      height: 50px;
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      align-items: center;
      position: relative;
      color: white;
      padding: 5px 0;
      background-color: rgb(41, 41, 41);
      border-bottom: 1px solid var(--theme-cell-black);
      .input {
        width: 75%;
        height: 80%;
        background-color: #494545;
        outline: none;
        border: none;
        text-indent: 30px;
      }

      .search-icon {
        color: rgb(164, 164, 164);
        position: absolute;
        left: 12px;
        top: 18px;
        font-size: 18px;
      }
    }
    p {
      height: 45px;
      line-height: 30px;
      width: 100%;
      padding: 8px;
      background-color: rgb(41, 41, 41);
      &:not(:last-child) {
        border-bottom: 1px solid var(--theme-cell-black);
      }
    }
    .friend-list {
      margin-top: 15px;

      .friend-item {
        height: 45px;
        line-height: 45px;
        width: 100%;
        background-color: rgb(24, 24, 24);
        display: flex;
        flex-direction: row;
        align-items: center;
        padding-left: 15px;
        .van-checkbox {
          padding-right: 10px;
        }
        .img {
          width: 50px;
          height: 40px;
          border-radius: 5px;
          margin-right: 15px;

          img {
            width: 100%;
            height: 100%;
            display: block;
            border-radius: 3px;
          }
        }

        .desc {
          width: 90%;
          border-bottom: 1px solid rgba(204, 203, 203, 0.1);
        }
      }
    }
  }

  .button {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 60px;
    padding: 10px;
    text-align: right;
    background-color: rgb(67, 67, 67);
    button {
      outline: 0px;
      border: 0px;
      padding: 8px 15px;
      color: #fff;
      border-radius: 5px;
      background-color: var(--theme-green);
    }
  }
}
</style>